#include "direct_product.h"

using std::string;
using std::vector;

namespace NSpellCheck {

void directProduct(const vector<vector<string> >& wordSuggestions, vector<Phrase>* combinations) {
    combinations->clear();
    int sizesProduct = 1;
    for (size_t i = 0; i < wordSuggestions.size(); ++i) {
        sizesProduct *= wordSuggestions[i].size();
    }
    if (sizesProduct == 0) {
        return;
    }
    for (int code = 0; code < sizesProduct; ++code) {
        vector<string> choices(wordSuggestions.size());
        int codeCopy = code;
        for (size_t i = 0; i < wordSuggestions.size(); ++i) {
            choices[i] = wordSuggestions[i][codeCopy % wordSuggestions[i].size()];
            codeCopy /= wordSuggestions[i].size();
        }
        combinations->push_back(choices);
    }
}

void directProduct(const vector<vector<Variation> >& wordSuggestions, vector<Variation>* combinations) {
    combinations->clear();
    int sizesProduct = 1;
    for (size_t i = 0; i < wordSuggestions.size(); ++i) {
        sizesProduct *= wordSuggestions[i].size();
    }
    if (sizesProduct == 0) {
        return;
    }
    for (int code = 0; code < sizesProduct; ++code) {
        vector<string> choices;
        double weight = 1.0;
        int codeCopy = code;
        for (size_t i = 0; i < wordSuggestions.size(); ++i) {
            int choiceIndex = codeCopy % wordSuggestions[i].size();
            const Variation& var = wordSuggestions[i][choiceIndex];
            choices.insert(choices.end(), var.phrase.begin(), var.phrase.end());
            weight *= var.weight;
            codeCopy /= wordSuggestions[i].size();
        }
        combinations->push_back(Variation(weight, choices));
    }
}


}
